﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace TSharp.Core.Mvc.Web
{
    /// <summary>
    ///     <para>2010/10/23</para>
    ///     <para>THINKPADT61</para>
    ///     <para>tangjingbo</para>
    /// </summary>
    public class TSharpController : Controller
    {
        /// <summary>
        /// </summary>
        public static string MessageInfoKey = "MessageBoxInfo";

        /// <summary>
        /// </summary>
        public static string MessageScriptKey = "MessageBoxInfo";

        /// <summary>
        ///     初始化调用构造函数后可能不可用的数据。
        /// </summary>
        /// <param name="requestContext">HTTP 上下文和路由数据。</param>
        protected override void Initialize(RequestContext requestContext)
        {
            //AopContext.Default.BuildUp(GetType(), this);
            base.Initialize(requestContext);
        }

        /// <summary>
        ///     当请求与此控制器匹配时调用，但在此控制器中找不到任何具有指定操作名称的方法。
        /// </summary>
        /// <param name="actionName">尝试的操作的名称。</param>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new Exception("没有找到对应的Action方法");
        }

        /// <summary>
        ///     Successes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Success()
        {
            return Success("成功");
        }

        /// <summary>
        ///     Successes the specified MSG.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns></returns>
        public ActionResult Success(string msg)
        {
            ViewData["Text"] = msg;
            return PartialView("Success");
        }

        /// <summary>
        ///     Successes the specified MSG.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="isReload">
        ///     if set to <c>true</c> [is reload].
        /// </param>
        /// <returns></returns>
        public PartialViewResult Success(string msg, bool isReload)
        {
            ViewData["Text"] = msg;
            if (isReload)
            {
                //ViewData["Text"] += "<script type='text/javascript'>setTimeout(function(){window.location.href=window.location.href;},500);</script>";
            }
            return PartialView("Success");
        }

        /// <summary>
        ///     Jsons the error.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns></returns>
        public ActionResult JsonError(string msg)
        {
            return JsonError(msg, false);
        }

        /// <summary>
        ///     Jsons the error.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="isText">
        ///     if set to <c>true</c> [is text].
        /// </param>
        /// <returns></returns>
        public ActionResult JsonError(string msg, bool isText)
        {
            var json = new Dictionary<string, object> {{"Error", true}, {"Text", msg}};
            return isText
                       ? Json(json, "text/html", JsonRequestBehavior.AllowGet)
                       : Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        ///     Gets the error.
        /// </summary>
        /// <param name="modelState">State of the model.</param>
        /// <returns></returns>
        [NonAction]
        protected string GetError(ModelStateDictionary modelState)
        {
            string errorMessage =
                modelState.Keys.Select(key => ModelState[key].Errors.FirstOrDefault())
                          .Where(error => error != null)
                          .Aggregate("", (current, error) => current + (error.ErrorMessage + "\r\n"));
            return errorMessage;
        }
    }
}